package org.example.wc;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * DataStream实现wordcount读取文件（无界流）
 *
 * docker exec -it ubuntu-socket apt-get update && apt-get install -y netcat vim
 * docker exec -it ubuntu-socket netcat -lk -p 7777
 * netcat -lk -p 7777
 */
public class WordCountStreamUnbounedDemo {

    public static void main(String[] args) throws Exception {
        //1.创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //2。读取数据 socket
        //DataStreamSource<String> socketDS = env.readTextFile("input/word.txt");
        DataStreamSource<String> socketDS = env.socketTextStream("192.168.43.55",7777);
        //3。处理数据 :切分、转投、分组、聚合
        SingleOutputStreamOperator<Tuple2<String, Integer>> sum = socketDS.flatMap(
                        (String value, Collector<Tuple2<String, Integer>> out) -> {
                            //按照 空格 切分
                            String[] words = value.split(" ");
                            for (String word : words) {
                                //通过采集器向下游发送数据
                                out.collect(Tuple2.of(word, 1));
                            }
                        })
                .returns(Types.TUPLE(Types.STRING, Types.INT))
                .keyBy((Tuple2<String, Integer> value) -> value.f0)
                .sum(1);
        //4。输出数据
        sum.print();
        //5。执行：类似sparkstreaming 最后ssc.start()
        env.execute();
    }
}
